﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Collections;
using System.Text;

namespace IService.Utility
{
	/// <summary>
	/// DBClass 的摘要说明。
	/// </summary>
	public class DBClass
	{
		//public System.Data.OleDb.OleDbConnection DBConn= new System.Data.OleDb.OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings["OraConnString"].ToString());
		public System.Data.OleDb.OleDbCommand DBCmd = new System.Data.OleDb.OleDbCommand();
		public System.Data.OleDb.OleDbDataAdapter DBDA = new System.Data.OleDb.OleDbDataAdapter();
		public System.Data.DataSet DS = new DataSet();
		public const string strNoConnection = "数据库不可用", strInsertError = "数据添加错误", strInsertSuccess = "数据添加成功"
			, strErrorSp = "存储过程执行错误", strUpdateSuccess = "数据更新成功"
			, strUpdateError = "数据更新失败";

		public DBClass()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}


		~DBClass()
		{
			try
			{
				DS.Dispose();
				DBDA.Dispose();
				DBCmd.Dispose();
			}
			catch
			{

			}
			//DBConn.Dispose();
		}


		#region 无返回结果的多个SQL语句操作(用动态数组传参数)
		public bool ExcuteSQL(string[] strInfo, System.Data.OleDb.OleDbConnection DBConn)
		{
			bool SuccFlag = false;
			DBConn.Open();
			System.Data.OleDb.OleDbTransaction st = DBConn.BeginTransaction();
			DBCmd.Transaction = st;
			try
			{
				DBCmd.Connection = DBConn;
				for (int i = 0; i < strInfo.Length; i++)
				{
					DBCmd.CommandText = strInfo[i];
					DBCmd.ExecuteNonQuery();
				}
				st.Commit();
				SuccFlag = true;
			}
			catch (Exception a)
			{
				string error = a.ToString();
				st.Rollback();
				SuccFlag = false;
			}
			finally
			{
				DBConn.Close();
			}
			return SuccFlag;
		}
		#endregion

		#region 无返回结果的多个SQL语句操作
		public bool ExcuteSQL(ArrayList strInfo, System.Data.OleDb.OleDbConnection DBConn)
		{
			bool SuccFlag = false;
			DBConn.Open();
			string sqlstr = "";

			System.Data.OleDb.OleDbTransaction st = DBConn.BeginTransaction();
			DBCmd.Transaction = st;
			try
			{
				DBCmd.Connection = DBConn;
				for (int i = 0; i < strInfo.Count; i++)
				{
					DBCmd.CommandText = strInfo[i].ToString();
					DBCmd.ExecuteNonQuery();
					sqlstr = i.ToString();
				}
				st.Commit();
				SuccFlag = true;
			}
			catch (Exception a)
			{
				sqlstr = sqlstr + ":" + a.ToString();
				string error = a.ToString();
				st.Rollback();
				SuccFlag = false;
			}
			finally
			{
				DBConn.Close();
			}
			return SuccFlag;
		}
		#endregion

		public ArrayList ExcuteSQL(ArrayList strInfo, bool ErrorExist, System.Data.OleDb.OleDbConnection DBConn)
		{
			ArrayList outArray = new ArrayList();

			DBConn.Open();
			string bb = "";
			int count = 0;
			outArray.Clear();
			System.Data.OleDb.OleDbTransaction st = DBConn.BeginTransaction();
			DBCmd.Transaction = st;
			try
			{

				for (int i = 0; i < strInfo.Count; i++)
				{
					try
					{
						DBCmd.CommandText = strInfo[i].ToString();
						//cmd.Transaction = st;					
						DBCmd.ExecuteNonQuery();
					}
					catch
					{
						if (ErrorExist == true)
						{
							count = count + 1;
							outArray.Add(i.ToString());

						}
					}
				}
				if (count != 0)
					st.Rollback();
				else
				{
					outArray.Add("true");
					st.Commit();
				}
				//SuccFlag=true;
			}
			catch (Exception a)
			{
				bb = bb + ":" + a.ToString();
				string error = a.ToString();
				st.Rollback();
				outArray.Add("false");
				//SuccFlag=false;
			}
			finally
			{
				DBConn.Close();
			}
			return outArray;
		}

		#region 执行一句SQL无返回
		public bool execute_SQL(string SQLstring, System.Data.OleDb.OleDbConnection DBConn)
		{
			bool SuccFlag = false;
			try
			{
				DBCmd.CommandText = SQLstring;
				DBCmd.Connection = DBConn;
				DBConn.Open();
				DBCmd.ExecuteNonQuery();
				DBConn.Close();
				return SuccFlag = true;
			}
			catch (Exception er)
			{
				string ReturnInfo = er.ToString();
				DBConn.Close();
				return SuccFlag;
			}
		}
		#endregion

		private int ReturnMaxID(string AValue, System.Data.OleDb.OleDbConnection DBConn)
		{
			try
			{
				DBConn.Open();
				DBCmd.CommandText = AValue;
				DBCmd.Connection = DBConn;

				System.Data.OleDb.OleDbDataReader dtrTmp;
				dtrTmp = DBCmd.ExecuteReader();//获得数据集

				int intTmp = 0;
				try
				{
					while (dtrTmp.Read())
					{
						intTmp = Convert.ToInt32(dtrTmp["MaxID"].ToString());
					}
				}
				catch
				{
					intTmp = 0;
				}
				intTmp++;

				return intTmp;
			}
			finally
			{
				DBConn.Close();
			}
		}


		#region 打开数据库
		public string connstring1(string inputstr)
		{
			string strConnect = inputstr;
			return strConnect;
		}
		public void OpenConn(string strConn, System.Data.OleDb.OleDbConnection DBConn)
		{
			DBConn.Open();
		}

		public void CloseConn(System.Data.OleDb.OleDbConnection DBConn)
		{
			DBConn.Close();
		}
		#endregion

		#region 返回 DataView
		public DataView GetOleDbDataView(string Sql, System.Data.OleDb.OleDbConnection DBConn)
		{
			DataView returnValue;
			System.Data.OleDb.OleDbDataAdapter myDataAdapter;
			DataSet myDataSet = new DataSet();
			myDataAdapter = new System.Data.OleDb.OleDbDataAdapter(Sql, DBConn);
			myDataAdapter.Fill(myDataSet, "test");
			returnValue = new DataView(myDataSet.Tables["test"]);
			return returnValue;
		}




		public string ReturnString(string AValue, System.Data.OleDb.OleDbConnection DBConn)
		{
			try
			{
				DBConn.Open();
				DBCmd.CommandText = AValue;
				DBCmd.Connection = DBConn;
				string strTmp = "";
				System.Data.OleDb.OleDbDataReader dtrTmp;
				dtrTmp = DBCmd.ExecuteReader();//获得数据集
				try
				{
					while (dtrTmp.Read())
					{
						strTmp = dtrTmp[0].ToString();
					}
				}
				catch
				{
					strTmp = "";
				}
				return strTmp;
			}
			finally
			{
				DBConn.Close();
			}

		}



		#endregion

		#region 执行一句SQL返回DataSet
		public DataSet GetDataSet(string Sql, System.Data.OleDb.OleDbConnection DBConn)
		{
			DataSet ds = new DataSet();
			try
			{
				DBConn.Open();

				DBDA.SelectCommand = new System.Data.OleDb.OleDbCommand(Sql.Trim(), DBConn);
				DBDA.Fill(ds);

				DBConn.Close();
			}
			catch (Exception err)
			{
				string error = err.ToString();
				DBConn.Close();
			}
			return ds;
		}
		#endregion

		#region 执行一句SQL返回一个值
		public object execute_SQLrv(string SQLstring, System.Data.OleDb.OleDbConnection DBConn)
		{
			object obj = new object();
			try
			{

				this.DBCmd.CommandText = SQLstring;
				this.DBCmd.Connection = DBConn;
				DBConn.Open();
				obj = this.DBCmd.ExecuteScalar();
				DBConn.Close();

			}
			catch
			{
				DBConn.Close();
			}
			return obj;
		}

		#endregion

		#region 获取最大ID信息
		public int GetIDInfo(string Sql, System.Data.OleDb.OleDbConnection DBConn)
		{
			int maxID = 1;
			DBCmd.CommandText = Sql;
			DBCmd.Connection = DBConn;
			System.Data.OleDb.OleDbDataReader read;
			DBConn.Open();
			read = DBCmd.ExecuteReader();
			bool Getflag = false;

			while (read.Read())
			{

				Getflag = true;
				try
				{
					maxID = Convert.ToInt32(read[0].ToString());
				}
				catch
				{
					maxID = 1;
				}
			}
			if (!Getflag)
			{
				maxID = 1;
			}
			DBCmd.Dispose();
			DBConn.Close();
			return maxID;
		}


		#endregion

		public static string InputText(string inputString, int maxLength)
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
			StringBuilder retVal = new StringBuilder();
			if ((inputString != null) && (inputString != String.Empty))
			{
				inputString = inputString.Trim();
				if (inputString.Length > maxLength)
					inputString = inputString.Substring(0, maxLength);
				for (int i = 0; i < inputString.Length; i++)
				{
					switch (inputString[i])
					{
						case '"':
							retVal.Append("&quot;");
							break;
						case '<':
							retVal.Append("&lt;");
							break;
						case '>':
							retVal.Append("&gt;");
							break;
						default:
							retVal.Append(inputString[i]);
							break;
					}
				}
				retVal.Replace("'", " ");
			}
			return retVal.ToString();
		}
	}
}